from machine import SPI
from typing import Optional

def spi_transfer(spi: SPI, data: bytes) -> Optional[bytes]:
    """
    Send and receive data via SPI
    """
    try:
        return spi.write_readinto(data, data)
    except Exception:
        return None
